home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Librarys / MMULib / C_Sources / MuFastZero.c < prev    next >
C/C++ Source or Header  |  1999-11-28  |  39KB  |  1,060 lines

  1. /*****************************************************************
  2.  ** MuFastZero                                                  **
  3.  **                                                             **
  4.  ** A MMU library compatible zeropage remapper                  **
  5.  ** Version 40.11 28.11.1999 © THOR-Software, by Thomas Richter **
  6.  *****************************************************************/
  7.  
  8. /// Includes
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <exec/ports.h>
  12. #include <exec/execbase.h>
  13. #include <dos/dos.h>
  14. #include <mmu/mmubase.h>
  15. #include <mmu/context.h>
  16. #include <mmu/mmutags.h>
  17. #include <workbench/startup.h>
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/mmu.h>
  21. #include <proto/dos.h>
  22. #include <proto/icon.h>
  23.  
  24. #include <string.h>
  25. ///
  26. /// Defines
  27. #define STRINGDATE "28.11.99"
  28. #define STRINGVERSION "40.11"
  29. #define CACHEFLAGS (MAPP_CACHEINHIBIT|MAPP_COPYBACK|MAPP_NONSERIALIZED|MAPP_IMPRECISE)
  30.  
  31. #define TEMPLATE "ON=FASTZERO/S,OFF=NOFASTZERO/S,FASTEXEC/S,FORCENATIVE/S,MOVESSP=FASTSSP/S,STACKSIZE/K/N"
  32.  
  33. #define OPT_ON  0
  34. #define OPT_OFF 1
  35. #define OPT_FASTEXEC 2
  36. #define OPT_FORCENATIVE 3
  37. #define OPT_FASTSSP 4
  38. #define OPT_STACKSIZE 5
  39. #define OPT_WINDOW 6
  40. #define OPT_COUNT 7
  41. ///
  42. /// Statics
  43. struct MMUBase *MMUBase;
  44. struct DosLibrary *DOSBase;
  45. struct ExecBase *SysBase;
  46. struct Library *IconBase;
  47.  
  48. void *_cw_to;
  49. void *_cw_from;
  50. ULONG _cw_size;
  51. ///
  52. /// Protos
  53. int __asm __saveds main(void);
  54. int BuildFastZero(LONG fastexec);
  55. int RemoveFastZero(void);
  56. ULONG ChipLowEnd(void);
  57. int KillFastZero(void);
  58. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  59. BOOL SetPages(struct MMUContext *ctx,ULONG props,ULONG lower,ULONG size,ULONG pagesize,ULONG dest);
  60. void CopyMMULess(void *to,void *from,ULONG size);
  61. ULONG __CopyWO();
  62. int MoveSSP(LONG size);
  63. void SuperStackSwap(UBYTE *newstack,ULONG size);
  64. ///
  65. /// Strukturen
  66. struct FastZeroPort {
  67.         struct MsgPort          frp_Port;
  68.         UWORD                   frp_cludgefill;
  69.         void                   *frp_Logical;            /* Logical position this is remapped to */
  70.         void                   *frp_Physical;           /* Physical location of the RAM used for remapping */
  71.         ULONG                   frp_Lower;              /* where remapping was started */
  72.         ULONG                   frp_Size;               /* size of the allocated memory */
  73.         struct Library         *frp_Base;               /* Keep the library open */
  74.         struct MemHeader       *frp_LowMemHeader;
  75.         ULONG                   frp_CacheFlags;         /* Keeps the caching mode for the remapping destination */
  76.         char                    frp_Name[32];           /* keeps the name of the port */
  77. };
  78.  
  79. /* This is the port build by MuMove4k */
  80.  
  81. struct MuMove4KPort {
  82.         struct MsgPort          mrp_Port;
  83.         UWORD                   mrp_cludgefill;
  84.         void                   *mrp_private;
  85.         struct MemHeader       *mrp_LowMemHeader;
  86.         ULONG                   mrp_UpperEnd;
  87. };
  88. ///
  89.  
  90. char version[]="$VER: MuFastZero " STRINGVERSION " (" STRINGDATE ") © THOR";
  91.  
  92. /// main
  93. int __asm __saveds main(void)
  94. {
  95. LONG args[OPT_COUNT];
  96. struct RDArgs *rd,*myrd;
  97. struct Process *proc;
  98. int rc=20;
  99. LONG err;
  100. struct WBStartup *msg;
  101. BPTR oldout;
  102. struct MsgPort *oldconsole;
  103. LONG stack;
  104.  
  105.  
  106.         SysBase=*((struct ExecBase **)(4L));
  107.  
  108.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  109.         /* Wait for the workbench startup, if any */
  110.         proc=(struct Process *)FindTask(NULL);
  111.  
  112.         if (!(proc->pr_CLI)) {
  113.                 WaitPort(&(proc->pr_MsgPort));
  114.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  115.         } else  msg=NULL;
  116.  
  117.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  118.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",41L)) {
  119.  
  120.                         myrd=NULL;      /* reset the temporary ReadArgs */
  121.                         oldout=NULL;
  122.                         oldconsole=NULL;
  123.                         if (msg) {
  124.                                 oldout=SelectOutput(NULL);
  125.                                 oldconsole=SetConsoleTask(NULL);
  126.                                 rd=ReadTTArgs(msg,args,&myrd);
  127.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  128.  
  129.                         if (rd) {
  130.                                 if (!GetMMUType()) {
  131.                                         Printf("MuFastZero requires a working MMU.\n");
  132.                                         err=10;
  133.                                 } else {
  134.                                         /* Argument parser worked, call main routine */
  135.                                         err=0;
  136.  
  137.                                         if (args[OPT_FORCENATIVE])
  138.                                                 err=KillFastZero();
  139.  
  140.                                         if (err<10 && ((args[OPT_ON]) || ((!args[OPT_OFF]) && (!args[OPT_FASTSSP]))))
  141.                                                         err=BuildFastZero(args[OPT_FASTEXEC]);
  142.  
  143.                                         if (err<10 && args[OPT_OFF])
  144.                                                 err=RemoveFastZero();
  145.  
  146.                                         if (err<10 && args[OPT_FASTSSP]) {
  147.                                                 if (args[OPT_STACKSIZE]) {
  148.                                                         stack=*(LONG *)args[OPT_STACKSIZE];
  149.                                                 } else  stack=0;
  150.                                                 err=MoveSSP(stack);
  151.                                         }
  152.                                 }
  153.  
  154.                                 FreeArgs(rd);
  155.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  156.                                 if (msg)  Close(SelectOutput(NULL));
  157.                         } else  err=IoErr();
  158.  
  159.                         if (msg) {
  160.                                 SelectOutput(oldout);
  161.                                 SetConsoleTask(oldconsole);
  162.                         }
  163.  
  164.                         if (err<64) {
  165.                                 rc=err;
  166.                                 err=0;
  167.                         } else {
  168.                                 if (!msg) PrintFault(err,"MuFastZero failed");
  169.                                 rc=10;
  170.                         }
  171.                         SetIoErr(err);
  172.  
  173.                         CloseLibrary((struct Library *)MMUBase);
  174.                 } else if (!msg) PrintFault(ERROR_OBJECT_NOT_FOUND,"MuFastZero requires the mmu.library V41 or better");
  175.                 CloseLibrary((struct Library *)DOSBase);
  176.         }
  177.  
  178.         if (msg) {
  179.                 Forbid();
  180.                 ReplyMsg((struct Message *)msg);
  181.         }
  182.  
  183.         return rc;
  184. }
  185. ///
  186. /// ReadTTArgs
  187. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  188. {
  189. struct WBArg *wbarg;
  190. struct DiskObject *dop;
  191. char **tt;                      /* ToolTypes array */
  192. char *wbstr;                    /* Our self-made workbench argument string */
  193. char *here;
  194. BPTR oldlock;
  195. ULONG len;
  196. struct RDArgs *rd=NULL,*myrd=NULL;
  197. LONG err=0;
  198. BPTR newout;
  199.  
  200.         if (IconBase=OpenLibrary("icon.library",37L)) {
  201.                 if (wbarg=msg->sm_ArgList) {
  202.                         /* use a project icon if there is one... */
  203.                         if (msg->sm_NumArgs > 1) wbarg++;
  204.  
  205.                         /* go into the directory */
  206.                         oldlock=CurrentDir(wbarg->wa_Lock);
  207.  
  208.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  209.                                 if (tt=dop->do_ToolTypes) {
  210.                                         /* Read a special tool type for the output window */
  211.  
  212.                                         /* Calc the size of the argument string */
  213.  
  214.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  215.                                         while (*tt) {
  216.                                                 len += strlen(*tt)+1;   /* string, plus space */
  217.                                                 tt++;
  218.                                         }
  219.  
  220.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  221.